package com.falcon.controller.toolkit;

import com.falcon.mvc.base.IBaseController;
import com.falcon.mvc.domain.BasePager;
import com.falcon.mvc.domain.BaseResponse;
import com.falcon.domain.dos.TableDO;
import com.falcon.domain.po.MysqlTablePO;
import com.falcon.domain.req.TableQuery;
import com.falcon.domain.req.TableRequest;
import com.falcon.domain.req.MysqlTableQuery;
import com.falcon.service.IMysqlTableService;
import com.falcon.service.ITableService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/toolkit/table")
@Tag(name = "TableController", description = "代码生成配置-接口")
public class TableController  implements IBaseController {

    @Autowired
    private ITableService tableService;

    @Autowired
    private IMysqlTableService mysqlTableService;

    @Operation(summary = "新增")
    @PostMapping("/save")
    public BaseResponse<Long> save(@Valid @RequestBody TableRequest request) {

        Long id =  this.tableService.save(request);

        return BaseResponse.success(id);
    }

    @Operation(summary = "修改")
    @PutMapping("/update/{id}")
    public BaseResponse<Boolean> update(@PathVariable Long id, @Valid @RequestBody TableRequest request) {

        Boolean success =  this.tableService.update(id, request);

        return BaseResponse.success(success);
    }

    @Operation(summary = "删除")
    @DeleteMapping("/delete/{id}")
    public BaseResponse<Boolean> deleteById(@PathVariable Long id) {

        Boolean success =  this.tableService.deleteById(id);

        return BaseResponse.success(success);
    }

    @Operation(summary = "批量删除")
    @PostMapping("/batchDelete")
    public BaseResponse<Integer> batchDelete(@RequestBody List<Long> ids) {

        Integer num =  this.tableService.batchDelete(ids);

        return BaseResponse.success(num);
    }

    @Operation(summary = "查询基本信息")
    @GetMapping("/{id}")
    public BaseResponse<TableDO> detail(@PathVariable Long id) {

        TableDO tableDo =  this.tableService.queryById(id);

        return BaseResponse.success(tableDo);
    }


    @Operation(summary = "列表查询")
    @PostMapping("/list")
    public BaseResponse<List<TableDO>> list(@RequestBody TableQuery request) {

        List<TableDO> list =  this.tableService.queryList(request);

        return BaseResponse.success(list);
    }

    @Operation(summary = "分页查询")
    @PostMapping("/pager")
    public BaseResponse<BasePager<TableDO>> pager(@RequestBody TableQuery request) {

        BasePager<TableDO> pager =  this.tableService.queryPager(request);

        return BaseResponse.success(pager);
    }

    @Operation(summary = "待导入表")
    @PostMapping("/candidateTables")
    public BaseResponse<List<MysqlTablePO>> candidateTables(@RequestBody MysqlTableQuery query) {

        List<MysqlTablePO> tables= this.mysqlTableService.queryNotImportTables(query);

        return BaseResponse.success(tables);
    }

    @Operation(summary = "批量导入数据表")
    @PostMapping("/importTables")
    public BaseResponse<Boolean> importTables(@RequestBody List<String> request) {

        this.mysqlTableService.importTables(request);

        return BaseResponse.success(true);
    }

    @Operation(summary = "表结构同步")
    @PostMapping("/sync/{id}")
    public BaseResponse<Long> sync(@PathVariable Long id) {
        this.mysqlTableService.sysTable(id);
        return BaseResponse.success(id);
    }

}
